package cn.calm.swing.osgi.generator.service;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import cn.calm.swing.osgi.api.DefaultProjectService;
import cn.calm.swing.osgi.api.util.EntityUtil;
import cn.calm.swing.osgi.generator.entity.GeneratorInfo;
import cn.calm.swing.osgi.generator.ui.Generator;

public class GeneratorProjectService extends DefaultProjectService {
	private static ImageIcon pojectIcon = new ImageIcon(
			GeneratorProjectService.class.getResource("/project.png"));
	private JPopupMenu menu=new JPopupMenu();
	public GeneratorProjectService(final BundleContext context) {
		JMenuItem edit=new JMenuItem("编辑");
		edit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				ServiceReference<Generator> serviceReference = context.getServiceReference(Generator.class);
				Generator service = context.getService(serviceReference);
				service.setProject(getProject());
				GeneratorInfo projectToEntity = EntityUtil.projectToEntity(getProject(), GeneratorInfo.class, "generatorInfo");
				service.setGeneratorInfo(projectToEntity);
				service.setVisible(true);
				context.ungetService(serviceReference);
			}
		});
		menu.add(edit);
	}

	@Override
	public JPopupMenu getPopupMenu() {
		return menu;
	}

	@Override
	public ImageIcon getIcon() {
		return pojectIcon;
	}
}
